<?php
/*------------------------------------------------------------------------
 * Base.php 
 *
 * 基类控制器
 * 	
 * Created on 2017-01-05
 *
 * Author: hwc123 <hwc123@139.com>
 * 
 * Copyright (c) 2017 All rights reserved.
 * ------------------------------------------------------------------------
 */

namespace app\index\controller;
use \think\Request;
use \think\Controller;

class Base extends Controller{
	public $_title     = '';// 完整标题
	public $_cur_title = '';// 当前标题
	public $_trail     = [];// 
	public $_pagesize  = 10;// 分页数量
	public $_user_id   = 0;// 用户ID
	public $_realname  = '';// 用户姓名
	public $_nickname  = '';// 用户昵称
	public $_mobile    = '';// 用户手机号
	
	function _initialize($is_user_login = false){

		//面包导航配置
		// $this->add_step('首页', url('/'));
		
		// 当前URL
		$this->_cur_url = $this->request->url(true);
		$this->assign('_cur_url', $this->_cur_url);
		
		// 网站配置
		$config = model('Config')->_get('', 'system');
		$this->assign('_config', $config);
		if($config['wap_url'] && $this->isMobile()){
			$this->redirect($config['wap_url']);
		}

		$this->user_id($is_user_login);

		$this->assign('controller_name', request()->controller());
		$this->assign('action_name', request()->action());
	}
	
	public function checkbox($model_name = '', $where = ''){
		$pk_val = $this->_get('pk_val', '主键val不能为空！');
		$pk_key = $this->_get('pk_key', '主键key不能为空！');
		$field  = $this->_get('field_name', '修改字段不能为空！');

		//使用模型
		$model = db($model_name ? $model_name : $this->request->controller());

		$where[$pk_key] = $pk_val;
		$value = $model->where($where)->value($field);

		$data[$field] = $value ? 0 : 1;
		$model->where($where)->update($data);

		$this->success('设置成功！', '');
	}

	protected function _get($name = '', $empty_msg = '', $default_value = '', $is_array_str = false){
		$value = input($name);
		$value = $this->common_field_check($value, $empty_msg, $default_value, $is_array_str);

		$tpl_value = htmlspecialchars($value);
		$this->assign(str_replace(array('/a'), '', $name), $tpl_value);

		return $value;
	}
	protected function _post($name = '', $empty_msg = '', $default_value = '', $is_array_str = false){
		$value = input($name);
		$value = $this->common_field_check($value, $empty_msg, $default_value, $is_array_str);

		$this->assign(str_replace(array('/a'), '', $name), $value);
		
		return $value;
	}
	/**
	 * 通用字段验证
	 * @param  string $field         参数名
	 * @param  string $empty_msg     值为空，提示信息
	 * @param  string $default_value 值为空，默认值
	 * @param  string $is_array_str  返回数组是否转为字符串，多个以逗号分隔
	 */
	protected function common_field_check($value, $empty_msg = '', $default_value = '', $is_array_str = false){
		if(is_array($value)){
			$array_val = implode(',', $value);
			$is_empty  = empty($array_val);
			$value     = $is_array_str ? $array_val : $value;
		}else{
			$is_empty = strlen(trim($value)) == 0;
			$value    = trim($value);
		}
		
		if($is_empty && $empty_msg){
			$this->error($empty_msg);
		}
		if($is_empty){
			$value = isset($default_value) ? $default_value : '';
		}

		return $value;
	}
	//操作步骤
	protected function add_step($title, $url = ''){
		$this->_title     = empty($this->_title) ? $title : $title . ' - ' . $this->_title;
		$this->_trail[]   = array('title'=>$title,'url'=>$url);
		$this->_cur_title = $title;
		return $this;
	}
	protected function view($tpl = ''){
		$this->assign('_title', $this->_title);
		$this->assign('_trail', $this->_trail);
		$this->assign('_cur_title', $this->_cur_title);

		return view($tpl);
	}
	// 请求模型
	protected function request(){
		return Request::instance();
	}
	// 是否为Ajax请求
	protected function isAjax(){
		return $this->request->isAjax();
	}
	// 是否为手机访问
	protected function isMobile(){
		return $this->request->isMobile();
	}
	// 是否为GET请求
	protected function isGet(){
		return $this->request->isGet();
	}
	// 是否为POST请求
	protected function isPost(){
		return $this->request->isPost();
	}


	//获取用户ID
	protected function user_id($is_login = true){
		$user = session('user_info');
		// 强制登录获取用户信息，超时则返回登录界面
		if($is_login){
			$user = model('User')->get_user_info($is_login);
		}

		$this->user                = $user;
		$this->_user_id            = $user['user_id'] ? $user['user_id'] : 0;
		$this->_user_show_name     = $user['show_name'] ? $user['show_name'] : 0;
		$this->_user_head_url      = $user['head_url'] ? $user['head_url'] : '';
		$this->_user_realname      = $user['realname'] ? $user['realname'] : '';
		$this->_user_mobile        = $user['mobile'] ? $user['mobile'] : '';
		$this->_user_email         = $user['email'] ? $user['email'] : '';
		$this->_user_nickname      = $user['nickname'] ? $user['nickname'] : '';
		$this->_user_province_id   = $user['province_id'] ? $user['province_id'] : 0;
		$this->_user_province_name = $user['province_name'] ? $user['province_name'] : 0;
		$this->_user_city_id       = $user['city_id'] ? $user['city_id'] : 0;
		$this->_user_city_name     = $user['city_name'] ? $user['city_name'] : 0;
		$this->_user_area_id       = $user['area_id'] ? $user['area_id'] : 0;
		$this->_user_type          = $user['user_type'] ? $user['user_type'] : 0;//用户类型
		$this->_user_sex           = $user['sex'] ? $user['sex'] : 0;//性别
		$this->_user_bind_info     = $user['bind_info'] ? $user['bind_info'] : '';//绑定信息
		$this->_is_set_password    = $user['is_set_password'];

		$this->_user_message_num = model('Message')->getUserMessageUnReadNum($this->_user_id);
		$this->assign('_user_message_num', $this->_user_message_num);

		$this->assign('user', $user);
		$this->assign('_user_id', $this->_user_id);
		$this->assign('_user_show_name', $this->_user_show_name);
		$this->assign('_user_type', $this->_user_type);
		$this->assign('_user_realname', $this->_user_realname);
		$this->assign('_user_email', $this->_user_email);
		$this->assign('_user_mobile', $this->_user_mobile);
		$this->assign('_is_set_password', $this->_is_set_password);
		$this->assign('_user_city_id', $this->_user_city_id);
		$this->assign('_user_head_url', $this->_user_head_url);

		return $user['user_id'] ? $user['user_id'] : 0;
	}

	// 获取地区数据
	public function get_area_list(){
		$where['parent_id'] = $this->_get('parent_id', '', 0);
		$list = model('Area')->where($where)->order('order_id,area_id')->select();
		$data['list'] = $list;

		$this->success('获取数据成功！', '', $data);
	}

	/**
	 * 获取短信验证码
	 */
	public function get_smscode(){
		$mobile  = $this->_get('mobile', '手机号不能为空！');
		$type    = $this->_get('type', '', 'register');
		$imgcode = $this->_get('imgcode', '图片验证码不能为空！');
		if(!captcha_check($imgcode)){
			$this->error('您输入图片验证码不正确！');
		}
		// $this->success('发送成功，请注意查收！');
		if(model('Sms')->send_smscode($mobile, $type)){
			$this->success('发送成功，请注意查收！');
		}else{
			$this->error(model('Sms')->tips_info);
		}
	}
}